package com.lqwu.mallchatserver.websocket.service.adapter;

import com.lqwu.mallchatserver.common.domain.enums.YesOrNoEnums;
import com.lqwu.mallchatserver.user.model.entry.User;
import com.lqwu.mallchatserver.websocket.model.enums.WSRespTypeEnum;
import com.lqwu.mallchatserver.websocket.model.vo.response.WsBaseResponse;
import com.lqwu.mallchatserver.websocket.model.vo.response.data.WSBlack;
import com.lqwu.mallchatserver.websocket.model.vo.response.data.WSLoginSuccess;
import com.lqwu.mallchatserver.websocket.model.vo.response.data.WSLoginUrl;
import com.thoughtworks.xstream.mapper.Mapper;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;

public class WebSocketAdapter {
    /**
     * 创建-登录二维码返回类型 的响应对象
     * @param wxMpQrCodeTicket
     * @return
     */
    public static WsBaseResponse<?> buildResp(WxMpQrCodeTicket wxMpQrCodeTicket) {
        // 新建 ws 响应对象（为登录二维码返回类型）
        WsBaseResponse<WSLoginUrl> response = new WsBaseResponse<>();
        response.setType(WSRespTypeEnum.LOGIN_URL.getType());
        response.setData(new WSLoginUrl(wxMpQrCodeTicket.getUrl()));
        return response;
    }

    /**
     * 创建-登录成功返回类型 的响应对象
     * @param user
     * @param token
     * @return
     */
    public static WsBaseResponse<?> buildResp(User user, String token, Boolean hasPower) {
        WsBaseResponse<WSLoginSuccess> response = new WsBaseResponse<>();
        response.setType(WSRespTypeEnum.LOGIN_SUCCESS.getType());
        WSLoginSuccess wsLoginSuccess = WSLoginSuccess.builder()
                .uid(user.getId())
                .avatar(user.getAvatar())
                .token(token)
                .name(user.getName())
                .power(hasPower ? YesOrNoEnums.Yes.getStatus() : YesOrNoEnums.NO.getStatus())
                .build();
        response.setData(wsLoginSuccess);
        return response;
    }

    /**
     * 创建-用户扫描成功等待授权返回类型 的响应对象
     * @return
     */
    public static WsBaseResponse<?> buildResp() {
        WsBaseResponse<WSLoginUrl> response = new WsBaseResponse<>();
        response.setType(WSRespTypeEnum.LOGIN_SCAN_SUCCESS.getType());
        return response;
    }

    /**
     * 创建-前端无效的token返回类型 的响应对象
     * @return
     */
    public static WsBaseResponse<?> buildInvalidTokenResp() {
        WsBaseResponse<WSLoginUrl> response = new WsBaseResponse<>();
        response.setType(WSRespTypeEnum.INVALIDATE_TOKEN.getType());
        return response;
    }

    /**
     *
     * @param user
     * @return
     */
    public static WsBaseResponse<?> buildBlack(User user) {
        WsBaseResponse<WSBlack> response = new WsBaseResponse<>();
        response.setType(WSRespTypeEnum.BLACK.getType());
        WSBlack wsBlack = WSBlack.builder()
                .uid(user.getId())
                .build();
        response.setData(wsBlack);
        return response;
    }
}
